package org.xczy.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.xczy.core.common.result.Result;
import org.xczy.core.entity.User;
import org.xczy.core.service.UserService;
import org.xczy.core.vo.LoginVo;
import org.xczy.core.vo.UserVo;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {

  @Resource
  private UserService userService;

  @ApiOperation("登录")
  @PostMapping("/login")
  public Result login(@RequestBody() LoginVo loginVo){
    Map<String, Object> data = userService.login(loginVo);
    if(data != null){
      return Result.ok(data);
    }
    return Result.fail("用户名或密码错误");
  }

  @ApiOperation("获取用户信息")
  @GetMapping("/info")
  public Result getUserInfo(@RequestHeader("token") String token){
    Map<String, Object> data = userService.getUserInfo(token);
    if(data != null){
      return Result.ok(data);
    }
    return Result.fail("身份验证过期，请重新登录");
  }

  @ApiOperation("登出")
  @PostMapping("/logout")
  public Result logout(@RequestHeader("token") String token){
    userService.logout(token);
    return Result.ok();
  }

  @ApiOperation("获取用户列表")
  @GetMapping("/list")
  public Result getUserList(
      @RequestParam(value = "username", required = false) String username,
      @RequestParam(value = "phone", required = false) String phone,
      @RequestParam(value = "email", required = false) String email,
      @RequestParam(value = "currentPage") Integer currentPage,
      @RequestParam(value = "size") Integer size
  ){
    LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(StringUtils.hasLength(username), User::getUsername, username);
    wrapper.eq(StringUtils.hasLength(phone), User::getPhone, phone);
    wrapper.eq(StringUtils.hasLength(email),User::getEmail, email);
    wrapper.orderByDesc(User::getId);
    size = size < 5 ? 5 : size < 100 ? size : 100;
    Page<User> page = new Page<>(currentPage, size);
    Page<User> userPage = userService.page(page, wrapper);
    Map<String, Object> map = new HashMap<>();
    map.put("total", userPage.getTotal());
    map.put("rows", userPage.getRecords());
    return Result.ok(map);
  }

  @ApiOperation("添加用户")
  @PostMapping("/add")
  public Result addUser(@RequestBody UserVo userVo){
    int i = userService.addUser(userVo);
    if(i > 0){
      return Result.ok("添加用户成功");
    }
    return Result.fail("该用户已存在");
  }

  @ApiOperation("修改用户")
  @PutMapping("/edit")
  public Result updateUser(@RequestBody User user){
    int i = userService.updateUser(user);
    if(i > 0){
      return Result.ok("修改用户成功");
    }
    return Result.fail("修改用户失败");
  }

  // todo 报错
  @ApiOperation("根据id查询用户")
  @GetMapping("/{id}")
  public Result getUserById(@PathVariable("id") Integer id) {
    User user = userService.getUserById(id);
    if(user != null){
      return Result.ok(user);
    }
    return Result.fail(null);
  }

  @ApiOperation("删除用户")
  @DeleteMapping("/{id}")
  public Result deleteUserById(@PathVariable("id") Integer id) {
    int i = userService.deleteUserById(id);
    if(i > 0){
      return Result.ok("删除成功");
    }
    return Result.fail("删除失败");
  }

}
